﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using quanlynhasach.Linksql;

namespace quanlynhasach
{
    public partial class frmNhanvien : Form
    {
        Nhanviensql nhanviensql = new Nhanviensql();
        Connection db = new Connection();
        int Count;
        public frmNhanvien()
        {
            InitializeComponent();
        }

        private Nhanvien getData()
        {
            Nhanvien nv = new Nhanvien();
            nv.MaNV = lbMaNV.Text;
            nv.TenNV = textBoxHoTen.Text;
            nv.NgaySinh = dateTimeNgaysinh.Value.ToString("MM/dd/yyyy");
            nv.SoCMND = textBoxSoCMND.Text;
            nv.SoDienThoai = textBoxSoDT.Text;
            nv.PassWord = textBoxMatkhau.Text;
            nv.DiaChi = textBoxDiaChi.Text;
            nv.Email = textBoxEmail.Text;
            nv.Loai = comboBoxQuyen.Text;
            nv.GioiTinh = comboBoxGioiTinh.Text;
            nv.Anh = pictureBox1.ImageLocation;
            return nv;
        }

        private void loadData()
        {
            DataTable NV = new DataTable();
            NV = nhanviensql.getNV();
            grdtNhanVien.DataSource = NV;      
        }

        private void Enable(bool a)
        {
            grdtNhanVien.Enabled = !a;
            btnThemNhanVien.Enabled = !a;
            btnSuaNhanVien.Enabled = !a;
            btnXoaNhanVien.Enabled = !a;
            btnLuuNhanVien.Enabled = a;
            btnHuyNhanVien.Enabled = a;
            textBoxHoTen.Enabled = a;
            textBoxDiaChi.Enabled = a;
            textBoxEmail.Enabled = a;
            dateTimeNgaysinh.Enabled = a;
            textBoxSoCMND.Enabled = a;
            textBoxSoDT.Enabled = a;
            comboBoxGioiTinh.Enabled = a;
            comboBoxQuyen.Enabled = a;
            textBoxMatkhau.Enabled = a;
            ChonAnh.Enabled = a;
            if (lbMaNV.Text.ToUpper() == frmMain.user.ToUpper())
                btnXoaNhanVien.Enabled = false;
        }

        private void reset()
        {
            textBoxHoTen.Text = "";
            textBoxDiaChi.Text = "";
            textBoxEmail.Text = "";
            dateTimeNgaysinh.Text = "1/10/1994";
            textBoxSoCMND.Text = "";
            textBoxSoDT.Text = "";
            comboBoxGioiTinh.Text = "Nam";
            comboBoxQuyen.Text = "Nhan vien";
            textBoxMatkhau.Text = lbMaNV.Text;
            pictureBox1.ImageLocation = "";
        }

        private string SearchDaTaGrid()
        {
            Count = 0;
            string MaNV = "";
            DataTable dt = new DataTable();//tao bang tam de luu
            dt = db.getDS("Select MaNV From NhanVien");
            for(int i=1 ; true;i++)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dt.Rows[j]["MaNV"].ToString() != "NV" + i)
                    {
                        Count++;//dem so lan khac
                    }
                    else
                    {
                        Count = 0;
                        break;
                    }
                }
                if (Count == dt.Rows.Count)// new so lan khac bang so hang cua bang, nghia la khong co dong nao trung thi tu dong add
                {
                    MaNV = "NV" + i;
                    break;
                }
            }
            return MaNV;
        }

        private void frmNhanvien_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            grdtNhanVien.DefaultCellStyle.ForeColor = Color.Blue;
            grdtNhanVien.AllowUserToAddRows = false;
            loadData();
            
        }

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {
            Enable(true);
            lbMaNV.Text = SearchDaTaGrid();
            reset();
        }

        private void btnHuyNhanVien_Click(object sender, EventArgs e)
        {
            Enable(false);
            loadData();
        }



        private void grdtNhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Rs = e.RowIndex;
            lbMaNV.Text = grdtNhanVien.Rows[Rs].Cells["MaNV"].Value.ToString();
            textBoxHoTen.Text = grdtNhanVien.Rows[Rs].Cells["TenNV"].Value.ToString();
            dateTimeNgaysinh.Text = grdtNhanVien.Rows[Rs].Cells["NgaySinh"].Value.ToString();
            textBoxSoCMND.Text = grdtNhanVien.Rows[Rs].Cells["SoCMND"].Value.ToString();
            textBoxSoDT.Text = grdtNhanVien.Rows[Rs].Cells["SoDienThoai"].Value.ToString();
            textBoxDiaChi.Text = grdtNhanVien.Rows[Rs].Cells["DiaChi"].Value.ToString();
            textBoxEmail.Text = grdtNhanVien.Rows[Rs].Cells["Email"].Value.ToString();
            comboBoxGioiTinh.Text = grdtNhanVien.Rows[Rs].Cells["GioiTinh"].Value.ToString();
            comboBoxQuyen.Text = grdtNhanVien.Rows[Rs].Cells["Loai"].Value.ToString();
            textBoxMatkhau.Text = db.getDS("Select PassWord from NhanVien where MaNV = '" + lbMaNV.Text + "'").Rows[0]["PassWord"].ToString();
            pictureBox1.ImageLocation = grdtNhanVien.Rows[Rs].Cells["Anh"].Value.ToString();
            if (lbMaNV.Text.ToUpper() == frmMain.user.ToUpper())
                btnXoaNhanVien.Enabled = false;
            else
                btnXoaNhanVien.Enabled = true;
        }

        private void btnSuaNhanVien_Click(object sender, EventArgs e)
        {
            Enable(true);
            if (lbMaNV.Text.ToUpper() == frmMain.user.ToUpper())
                comboBoxQuyen.Enabled = false;
        }

        private void btnXoaNhanVien_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc là xóa nhân viên " + lbMaNV.Text +
                " này không ?. Nếu xóa, tất cả dữ liệu liên quan sẽ bị xóa theo.", "Xác nhận",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                if (nhanviensql.XoaNV(lbMaNV.Text))
                    MessageBox.Show("Xóa thành công !");
            loadData();
        }

        private void btnLuuNhanVien_Click(object sender, EventArgs e)
        {
            Nhanvien nv = getData();
            if (nhanviensql.check(nv.MaNV))
                if (nhanviensql.SuaNV(nv))
                    MessageBox.Show("Cập nhật thành công");
                else
                    MessageBox.Show("Xem lại !");
            else
                if (nhanviensql.ThemNV(nv))
                    MessageBox.Show("Thêm thành công !");
                else
                    MessageBox.Show("Xem lại !");
            loadData();
            Enable(false);
        }

        private void ChonAnh_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureBox1.ImageLocation = openFileDialog1.FileName;
        }
    }
}
